//    Copyright (c) 2009 Elwyn Malethan
//
//    This file is part of SeemoreJ.
//
//    SeemoreJ is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    SeemoreJ is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with SeemoreJ.  If not, see <http://www.gnu.org/licenses/>.

package com.malethan.seemorej;

import com.malethan.seemorej.context.Context;
import com.malethan.seemorej.rendering.JspRendering;
import com.malethan.seemorej.rendering.ViewRendering;
import com.malethan.seemorej.rendering.TextRendering;
import com.malethan.seemorej.rendering.BlankRendering;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Provides useful access to the http related objets</p>
 */
public class SeemoreJ {
    //----------------------------------------------------------------------- Static Properties and Constants

    private static final ThreadLocal<Context> context = new ThreadLocal<Context>();

    public static final String FORMAT = "_sjFormat";
    public static final String ACTION = "_sjAction";
    public static final String CONTROLLER = "_sjController";

    //----------------------------------------------------------------------- Static Methods

    static Context getContext() {
        return context.get();
    }

    public static HttpServletRequest request() {
        return getContext().getRequest();
    }

    public static HttpServletResponse response() {
        return getContext().getResponse();
    }

    /**
     * Populates a given object's properties with values from the request.
     * 
     * @param prefix If the prefix is 'myBean' then all parameters in the request matching 'myBean.*' will be
     * consulted for values.
     * @param bean the object whose properties should be set.
     */
    public static void populateBean(String prefix, Object bean) {
        getContext().populateBean(prefix, bean);
    }

    public static void populateBean(Object bean) {
        getContext().populateBean(bean);
    }

    public static String flash(Flash type) {
        return getContext().flash(type);
    }

    public static void flash(Flash type, String value) {
        getContext().flash(type, value);
    }

    protected static void init(Context context) {
        SeemoreJ.context.set(context);
    }

    protected static void free() {
        SeemoreJ.context.remove();
    }

    public static void renderJsp(String jspPath) {
        getContext().setRendering(new JspRendering(jspPath));
    }

    public static void renderView(String view) {
        getContext().setRendering(new ViewRendering(view));
    }

    public static void renderText(String text) {
        getContext().setRendering(new TextRendering(text));
    }

    public static void renderNothing() {
        getContext().setRendering(new BlankRendering());
    }

    //----------------------------------------------------------------------- Instance Properties
    //----------------------------------------------------------------------- Constructors
    //----------------------------------------------------------------------- Getters and Setters
    //----------------------------------------------------------------------- Instance Methods
}
